package com.competition.vod.service.impl;


import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.competition.servicebase.exceptionhandler.MyException;
import com.competition.vod.Utils.ConstantVodUtils;
import com.competition.vod.Utils.InitVodClient;
import com.competition.vod.service.VodService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {


    @Override
    public String uploadVideoAly(MultipartFile file) {
        String fileName=file.getOriginalFilename();//文件本身的名字
        String title="";//上传后的文件名字(这里是截取文件后缀名后的数据)
        title=fileName.substring(0,fileName.lastIndexOf("."));
        try {
            InputStream inputStream=file.getInputStream();
            String vodId=uploadStream(ConstantVodUtils.ACCESS_KEY_ID,ConstantVodUtils.ACCESS_KEY_SECRET,title,fileName,inputStream);
            return vodId;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void removeMoreAlyVideo(List<String> videoIdList) {
        String ids= StringUtils.join(videoIdList.toArray(),",");//拼接字符串
        System.out.println(ids);
        try {
            //初始化对象
            DefaultAcsClient defaultAcsClient = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建删除视频的request对象
            DeleteVideoRequest deleteVideoRequest=new DeleteVideoRequest();
            //向request对象设置id
            deleteVideoRequest.setVideoIds(ids);
            //调用初始化方法删除视频id
            defaultAcsClient.getAcsResponse(deleteVideoRequest);
        }catch (Exception e){
            e.printStackTrace();
            throw new MyException(20001,"删除视频失败");
        }
    }

    //流式上传接口
    /*String accessKeyId,
     String accessKeySecret,
     String title, 上传后显示的名称
     String fileName, //上传前的文件名称
     InputStream inputStream//文件输入流
     */
    private static String uploadStream(String accessKeyId, String accessKeySecret, String title, String fileName, InputStream inputStream) {
        UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
        System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
        if (response.isSuccess()) {
            System.out.print("VideoId=" + response.getVideoId() + "\n");
            return response.getVideoId();//返回videoId
        } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
            System.out.print("VideoId=" + response.getVideoId() + "\n");
            System.out.print("ErrorCode=" + response.getCode() + "\n");
            System.out.print("ErrorMessage=" + response.getMessage() + "\n");
        }
        return null;
    }

}
